﻿@using IOA.MES.DataAccess.VModel;
@using IOA.MES.DataAccess.Enum;

@{
	ViewBag.Title = "工单结转列表";
	Layout = "~/Views/Shared/_LayoutPage.cshtml";

	var workShops = ViewBag.WorkShops as List<VWorkshop>;
	var statusEnums = ViewBag.CarryOverStatusEnums as List<KeyValuePair<int, string>>;
}

<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">车间：</label>
					<div class="layui-input-inline">
						<select name="workshopCode">
							<option value="">所有车间</option>
							@foreach (var workShop in workShops)
							{
								<option value="@workShop.WorkshopCode">@workShop.WorkshopName</option>
							}
						</select>
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">转出工单：</label>
					<div class="layui-input-inline">
						<input name="fromWorkOrderNo" class="layui-input" placeholder="请输入工单号" autocomplete="on">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">转入工单：</label>
					<div class="layui-input-inline">
						<input name="toWorkOrderNo" class="layui-input" placeholder="请输入工单号" autocomplete="on">
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">结转日期：</label>
					<div class="layui-input-inline">
						<input name="createdTimeRange" class="layui-input" placeholder="请选择结转日期" autocomplete="off">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">状态：</label>
					<div class="layui-input-inline">
						<select name="status">
							@*@foreach (var status in statusEnums)
								{
									<option value="@status.Key">@status.Value</option>
								}*@
							<option value="">全部状态</option>
							<option value="0">待确认</option>
							<option value="1">已结转</option>
						</select>
					</div>
				</div>

				<div class="layui-inline">
					<button class="layui-btn layui-btn-normal" lay-submit lay-filter="search">查询</button>
					<button class="layui-btn layui-btn-normal" onclick="createCarryOverOrder();">新建</button>
				</div>
			</div>
		</div>
	</div>

	<div class="layui-card">
		<div class="layui-card-body">
			<table id="tbCarryOver" lay-filter="tableEvent"></table>
		</div>
	</div>
</div>

<script id="carryOverOrder" type="text/html">
	<div class="layui-form-item" style="margin-top:20px;">
		<label class="layui-form-label"><i>*</i>转出工单：</label>
		<div class="layui-inline" style="width:260px;">
			<input id="txtFromWorkOrderNo" class="layui-input" name="FromWorkOrderNo" lay-verify="required|FromWorkOrderNo" lay-verType="tips" autocomplete="on" placeholder="请填写转出工单" />
		</div>
	</div>
	<div class="layui-form-item">
		<label class="layui-form-label"><i>*</i>转入工单：</label>
		<div class="layui-inline" style="width:260px;">
			<input id="txtToWorkOrderNo" class="layui-input" name="ToWorkOrderNo" lay-verify="required|ToWorkOrderNo" lay-verType="tips" autocomplete="on" placeholder="请填写转出工单" />
		</div>
	</div>

	<div class="layui-form-item" style="text-align:center;margin-bottom:0;margin-top:30px;">
		<div class="layui-inline">
			<button class="layui-btn layui-btn-normal" onclick="saveCarryOverOrder();">确定</button>
			<button class="layui-btn" style="background-color:#CCC;" onclick="cancelCreateCarryOverOrder();">取消</button>
		</div>
	</div>
</script>

@section Scripts{
	<script>
		var table;
		layui.use(['table', 'form'], function () {
			table = layui.table;
			var form = layui.form;

			var options = {
				elem: '#tbCarryOver'
				, url: '/WorkOrder/QueryWorkOrderList'
				, method: "post"
				, page: true
				, limit: 15
				, limits: [15, 20, 25, 30, 40, 50]
				, response: {
					statusName: 'ErrCode'
					, statusCode: 200
					, msgName: 'ErrCode'
					, countName: 'TotalCount'
					, dataName: 'Data'
				},
				where: {
				}
				, cols: [[
					{ type: 'numbers', title: '序号', width: 60, align: 'center' }
                    , { field: 'CarryOverNo', title: '结转单号', minWidth: 120, align: 'center' }
                    , { field: 'FromWorkOrderNo', title: '转出单号', minWidth: 200, align: 'center' }
                    , { field: 'ToWorkOrderNo', title: '转入工单', minWidth: 200, align: 'center' }
                    , { field: 'CNCreatedBy', title: '操作人', minWidth: 80, align: 'center' }
                    , { field: 'CNCreatedTime', title: '操作时间', minWidth: 150, align: 'center' }
                    , { field: 'CNStatus', title: '状态', minWidth: 80, align: 'center' }
					, {
						title: '操作', align: 'center', width: 250, fixed: 'right', templet: function (d) {
							var htmlStr = '';
							if (d.Status == '@((int)CarryOverOrderStatus.UnConfirmed)') {
								htmlStr = `
											<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
											<a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="carryOver"><i class="layui-icon layui-icon-auz"></i>确认结转</a>
											<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>作废</a>
										  `;
							} else if (d.Status == '@((int)CarryOverOrderStatus.Confirmed)') {
								htmlStr = `<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="detail"><i class="layui-icon layui-icon-email"></i>结转详情</a>`;
							}

							return htmlStr;
						}

					}
				]]
			};
			var tableIns = table.render(options);

			table.on('tool(tableEvent)', function (obj) {
				var event = obj.event;
				var data = obj.data;

				if (event == 'edit') {
					parent.layui.index.openTabsPage("/WorkOrder/CarryOverDetail?pkId=" + data.PkId + '&isReadonly=0', '结转编辑');
				} else if (event == 'carryOver') {
					layer.confirm('确认结转？', { icon: 3, title: '提示' }, function (index) {
						$.ajax({
							url: '/WorkOrder/CarryOver',
							data: {
                                carryOverNo: data.CarryOverNo
							},
							dataType: 'json',
							success: function (res) {
								if (res.ErrCode != 200) {
									return layer.msg(res.ErrMsg, { icon: 5 });
								}

								layer.msg('结转成功', { icon: 6 });
								layer.close(index);
								tableIns.reload('tbCarryOver', {
									page: {
										curr: 1
									}
								});
							}
						});
					});
				} else if (event == 'delete') {
					layer.confirm('确定作废该结转工单', { icon: 3, title: '提示' }, function (index) {
						$.ajax({
							url: '/WorkOrder/DeleteCarryOverOrder',
							data: {
								carryOverNo: data.CarryOverNo
							},
							dataType: 'json',
							success: function (res) {
								if (res.ErrCode != 200) {
									return layer.msg(res.ErrMsg, { icon: 5 });
								}

								layer.msg('作废成功', { icon: 6 });
								layer.close(index);
								tableIns.reload('tbCarryOver', {
									page: {
										curr: 1
									}
								});
							}
						});
					});
				} else if (event == 'detail') {
					parent.layui.index.openTabsPage("/WorkOrder/CarryOverDetail?pkId=" + data.PkId + '&isReadonly=1', '结转详情');
				}
			});

			form.on('submit(search)', function (d) {
				var data = d.field;
				options.where.workshopCode = data.workshopCode.trim();
				options.where.fromWorkOrderNo = data.fromWorkOrderNo.trim();
				options.where.toWorkOrderNo = data.toWorkOrderNo.trim();
				options.where.createdTimeRange = data.createdTimeRange.trim();
				options.where.status = data.status;

				table.reload('tbCarryOver',options);
			});

			form.on('submit(saveCarryOverOrder)', function (d) {
				$.ajax({
					url: '/WorkOrder/CreateCarryOverOder',
					data: d.field,
					dataType: 'json',
					success: function (res) {
						if (res.ErrCode != 200) {
							return layer.msg(res.ErrMsg, {icon:5});
						}

						layer.msg('保存成功', {icon:6});
					}
				});
			});
		});

		// 创建结转单
		var index;
		function createCarryOverOrder() {
			index = layer.open({
				type: 1,
				title: '创建结转单',
				area: ['450px', '270px'],
				content: carryOverOrder.innerHTML,
				end: function () {
					table.reload('tbCarryOver', {
						page: {
							curr: 1
						}
					});
				}
			});
		}

		function saveCarryOverOrder() {
			var fromWorkOrderNo = $('#txtFromWorkOrderNo').val().trim(),
				toWorkOrderNo = $('#txtToWorkOrderNo').val().trim();
			if (!fromWorkOrderNo) {
				return layer.msg('请输入转出工单', { icon: 5 });
			} else if (!toWorkOrderNo) {
				return layer.msg('请输入转入工单', { icon: 5 });
			}

			$.ajax({
				url: '/WorkOrder/CreateCarryOverOder',
				data:
				{
					FromWorkOrderNo: fromWorkOrderNo,
					ToWorkOrderNo: toWorkOrderNo
				},
				dataType: 'json',
				success: function (res) {
					if (res.ErrCode != 200) {
						return layer.msg(res.ErrMsg, {icon:5});
					}

					layer.msg('保存成功', { icon: 6 });
					layer.close(index);
				}
			});
		}

	// 取消创建结转单
	function cancelCreateCarryOverOrder() {
		layer.close(index);
	}
	</script>
}
